Updating an old binary sensor

So I recently did a major update to HA and stepped through multiple versions. I got so far behind because HA was so damn stable that everything just worked.

Anyways I had an old binary sensor that was created with a value template. It is no longer working as the sensors the value template now have a value when they are off, previously they had no value assigned.

Here is the old binary_sensor:

  - platform: template
    sensors:
      bad_weather:
        value_template: >-
          {% set alerts = [ is_state('sensor.advisories', ""),
                            is_state('sensor.statements', ""),
                            is_state('sensor.watches', ""),
                            is_state('sensor.warnings', "")] %}
          {{ alerts | select('==', false) | list | count > 0  }}

I think I need something like this but I’m not even sure about that.

  - template:
    binary_sensor:
      name: "bad_weather"
      state: "{{ }}"

Basically I need to check sensor.advisories, sensor.statements, sensor.watches, and sensor.warnings and if any of them have a value that isn’t ‘0’ the 'bad_weather binary sensor is set to ‘on’ if all of the sensor’s have a value of ‘0’ then bad_weather is set to ‘off’.

I think that you need to also remove the dash in front of “template:” since template is a top level key.

template:
  - binary_sensor:
      - name: Bad Weather
        state: >
          {{ [ 'sensor.advisory', 'sensor.statements', 'sensor.watches', 'sensor.warnings' ]
              | map('states') | select('eq', '0') | list | count < 4 }}
1 Like

@123 @finity Thanks for the help.
Turns out I at some point had gone a completely different direction with my weather alert card and I forgot to remove the bad_weather binary sensor.

Here is what I ended up with,

type: custom:state-switch
entity: template
template: >-
  {% if states('sensor.warnings') != '0'  %} show-1 {% elif
  states('sensor.watches') != '0'  %} show-2 {% elif states('sensor.statements')
  != '0'  %} show-3 {% elif states('sensor.advisories') != '0'  %} show-4 {%
  else %} dont_show {% endif %}
states:
  show-1:
    type: markdown
    content: >-
      Environment Canada has issued a Warning<br /> <a
      href="https://weather.gc.ca/warnings/report_e.html?xxxx">Environment
      Canada Warning</a>
    title: Weather Warning
    theme: weather_warning
  show-2:
    type: markdown
    content: >-
      Environment Canada has issued a Watch<br /> <a
      href="https://weather.gc.ca/warnings/report_e.html?xxxx">Environment
      Canada Watch</a>
    title: Weather Watch
    theme: weather_watch
  show-3:
    type: markdown
    content: >-
      Environment Canada has issued a Statement<br /> <a
      href="https://weather.gc.ca/warnings/report_e.html?xxxx">Environment
      Canada Statement</a>
    title: Weather Statement
    theme: weather_statement
  show-4:
    type: markdown
    content: >-
      Environment Canada has issued an Advisory<br /> <a
      href="https://weather.gc.ca/warnings/report_e.html?xxxx">Environment
      Canada Advisory</a>
    title: Weather Advisory
1 Like