# Conditional sections in Raspberry Pi config.txt for Composite video support

**URL:** <https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063>\
**Category:** Product support\
**Tags:** raspberrypi4\
**Created:** [January 13, 2021, 10:14pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063 "2021-01-13T22:14:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilnz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/neilnz/32/4999_2.png) [@neilnz](https://forums.balena.io/u/neilnz)\
**Post date:** [January 13, 2021, 10:14pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/1 "2021-01-13T22:14:42Z")

</div>

Hi,

We’re trying to deploy a product on the Pi 4 that needs to work with either HDMI or Composite output, depending on what’s connected at boot. It turns out on the pi4 this isn’t possible without using conditional config (see [https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=257078](https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=257078) for details). On previous Pi models this auto-switching was the default behaviour.

In our image, we’ve modified (part of) config.txt to read like this:

```
enable_tvout=1
sdtv_mode=2

[EDID=*]
enable_tvout=0

```

and this works fine - the pi will disable composite if an HDMI display is present at boot. Note if enable\_tvout=1 then the pi4 ignores whatever is connected to HDMI, hence the need for this hack.

The problem is the supervisor code that manages config.txt doesn’t understand conditional sections and when it does the sync to config variables, it rewrites the file without the conditional config, and we lose HDMI support again.

I’ve had a look through the code in [https://github.com/balena-io/balena-supervisor/blob/e5a1561bff19741534abd1f2d1b7ec59b1d29f44/src/config/backends/config-txt.ts](https://github.com/balena-io/balena-supervisor/blob/e5a1561bff19741534abd1f2d1b7ec59b1d29f44/src/config/backends/config-txt.ts) to see if there’s any support for conditional blocks or even to totally disable the overwriting of config.txt, and come up short. The only possibility I can think of is to intentionally corrupt an array-syntax config variable to cause the whole thing to throw an exception.

Does anyone have any other ideas? I don’t see any pathway to stop the supervisor destroying the config without it gaining some new significant smarts… and all the alternatives to dynamically rewrite config vars from the application are going to be really ugly. In particular, the tvservice command can’t enable composite on the pi4 if it wasn’t enabled at boot, so that won’t work either…

Thanks for any help on offer…

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [January 15, 2021, 1:23pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/19 "2021-01-15T13:23:11Z")

</div>

hello @neilnz

As you guessed, the supervisor does not support setting conditional configs or disabling overwriting the `config.txt` file

My colleague suggested that a workaround could be that you have a service that detects the output and change the `BALENA_HOST_CONFIG_enable_tvout` through the API.

Is that something that would work for you?

---

<div class="post-metadata">

**Author:** ![neilnz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/neilnz/32/4999_2.png) [@neilnz](https://forums.balena.io/u/neilnz)\
**Post date:** [January 17, 2021, 10:18pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/21 "2021-01-17T22:18:59Z")

</div>

Hi @rahul-thakoor,

Thanks for the reply.

I thought about something like this, but as far as I can tell it would be a one-way thing - detect no HDMI on boot, set `enable_tvout=1`, then it’s done and could never go back. If HDMI came back on another boot it wouldn’t be detected, because when `enable_tvout=1` there’s no hotplug events on HDMI and probing with eg. `tvservice -s` or `tvservice -l`doesn’t see anything attached to HDMI. This is quite unlike the previous Pi generations where it was possible to switch between composite and HDMI at runtime using the `tvservice` command. I do wonder if a future update to the Pi firmware might repair this, though it appears composite output support is a much lower priority on the Pi 4, and enabling it has to change the system clocks. I wonder if that causes a problem for the HDMI clock that forces them to disable the HDMI port…

---

<div class="post-metadata">

**Author:** ![the-real-kenna](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/the-real-kenna/32/9598_2.png) [@the-real-kenna](https://forums.balena.io/u/the-real-kenna)\
**Post date:** [January 21, 2021, 9:44pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/22 "2021-01-21T21:44:24Z")

</div>

Hi @neilnz, that makes sense. Unfortunately that’s the best option we have at this time, but we’ve taken your feedback and created a GitHub issue to track other users’ need for conditional configurations, which you can keep an eye on here: [https://github.com/balena-io/balena-supervisor/issues/1573](https://github.com/balena-io/balena-supervisor/issues/1573)

---

<div class="post-metadata">

**Author:** ![neilnz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/neilnz/32/4999_2.png) [@neilnz](https://forums.balena.io/u/neilnz)\
**Post date:** [January 21, 2021, 10:40pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/24 "2021-01-21T22:40:17Z")

</div>

Yep sounds fair. As a stopgap would you consider a config setting that locks config.txt from being updated by the supervisor? Manually managing this on our devices is going to be difficult because we won’t know which ones are meant to be which setting ahead of time.

---

<div class="post-metadata">

**Author:** ![dtischler](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/dtischler/32/2483_2.png) [@dtischler](https://forums.balena.io/u/dtischler)\
**Post date:** [January 21, 2021, 11:14pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/25 "2021-01-21T23:14:36Z")

</div>

Well, the biggest issue in that scenario that I can see, is that we would be reverting functionality that is fully intended to exist. The Supervisor is designed from the onset to be able to perform those changes, so, I am not sure that adding functionality to actively prevent it form happening is is the right path forward. The second piece of the puzzle here is that the Supervisor exists across _all_ device types, but _only_ Raspberry Pi’s include the config.txt, so there is added complexity there as well. I think the hangup is really the behavior of the Pi4…could the Pi3 suffice for your project since it seems to be able to do the automatic switching in a superior way?

---

<div class="post-metadata">

**Author:** ![neilnz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/neilnz/32/4999_2.png) [@neilnz](https://forums.balena.io/u/neilnz)\
**Post date:** [January 25, 2021, 8:38pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/27 "2021-01-25T20:38:25Z")

</div>

Hi @dtischler,

The pi4 was picked because of dual HDMI (another requirement…) so while the project runs fine on the pi3, the pi4 was purchased in bulk and it’s what we have.

I recognise the config mechanism exists across device types. In the supervisor, the config.txt handling is its own class, and there’s pi-isms in there already. I understand the issues with the supervisor not being allowed to write config, as it in some instances is ensuring a minimum set, and OS upgrades sometimes need to twiddle the config too. I’m stuck in a rather difficult place here as I need to get this working, and I’d rather not have to run a forked supervisor to do it, though I do understand the openbalena route is increasingly there to cater to us that are off the beaten path.

The latest version of the supervisor actually crashes the entire supervisor when it meets a conditional config section in the existing config.txt (can’t handle the “[” leading a line) which is a bit unfortunate.

Another issue is that colon isn’t allowed in a device config variable by Balena API, making it impossible to set any config for the second HDMI port (eg. hdmi\_mode:1=31) so it’s no longer possible to reach all the possible pi4 config options already. That one seems easy to fix though.

For now I’m forging on with running a separate Balena application for composite out devices and keeping the fleet config separate as a solution, but I’d like to reunify the fleet at some point when it’s possible to.

Thanks

---

<div class="post-metadata">

**Author:** ![klutchell](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/klutchell/32/861_2.png) [@klutchell](https://forums.balena.io/u/klutchell)\
**Post date:** [January 26, 2021, 5:26pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/29 "2021-01-26T17:26:12Z")

</div>

Hey @neilnz, I updated the existing github issue regarding conditional config fields, and created a new one for colons in device config variables here:

[https://github.com/balena-io/balena-io/issues/2338](https://github.com/balena-io/balena-io/issues/2338)

I suggest enabling notifications on those two issues so you can see when we make progress!

---

<div class="post-metadata">

**Author:** ![neilnz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/neilnz/32/4999_2.png) [@neilnz](https://forums.balena.io/u/neilnz)\
**Post date:** [January 27, 2021, 10:11pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/31 "2021-01-27T22:11:56Z")

</div>

I don’t think I can access that balena-io issue, it seems to be on a project that’s not public. I’m on the first one though.

---

<div class="post-metadata">

**Author:** ![Ereski](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ereski](https://forums.balena.io/u/Ereski)\
**Post date:** [January 28, 2021, 5:21pm UTC](https://forums.balena.io/t/conditional-sections-in-raspberry-pi-config-txt-for-composite-video-support/234063/34 "2021-01-28T17:21:02Z")

</div>

Sorry, the previous link is in an internal repository as it is an overarching issue. We’ll post another link if this issue moves into a public repo.
