How to add custom binary sensor?

Sorry, this is a basic question, but I’m unable to figure it out.

I want to add a custom binary sensor whose value is derived from an attribute of another sensor.

Following this recipe (in Norwegian, sorry): https://skagmoelectronics.com/page.php?p=documents/06_tibber_ha_stromsparing

I’m supposed to add this yaml to the configuration:

- platform: template
  sensors:
    power_saving:
      value_template: "{{ states.sensor.electricity_price_[din_adresse].attributes.price_level == 'VERY_EXPENSIVE' }}"

Problem is that when I add above yaml to my configuation.yaml I get an error message: “end of the stream or a document separator is expected at line 14, column 1” (line 14, column 1 being the first character in the recipe).

I’m guessing maybe something has changed since the article was written? I need to figure out where to add these lines.

My configuration.yaml looks like this:

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

Give it a try with the new method. Template - Home Assistant

First: I get ( Warning: Potential Security Risk Ahead ) when clicking your link, most likely they just don’t have valid certificate

Second: Do you have Tibber integration ?
… and is that really the name of your entity ?, or just something you copied/paste )

Anyways: The answer to your question is

Replace:

value_template: "{{ states.sensor.electricity_price_[din_adresse].attributes.price_level == 'VERY_EXPENSIVE' }}"

With:

value_template: "{{ state_attr('sensor.din_elpris_sensor','price_level') }}"

That is, if you want the Value from this sensors attribute:

If you want this template to show “true” or “on” if sensor.price.attribute.level == ‘VERY_EXPENSIVE’ it’s an other story :wink:

you can also make it show Problem :rofl: Simply add binary_class: Problem

Thanks! With this info I was finally able to make it work.

The certificate has probably just expired. I do have the Tibber integration and I was going to replace my address :slight_smile:

I was also able able to add a binary sensor which I will use to turn off my water heater when the price is very expensive.

Thank you so munch for your help, this was much appreciated! :slight_smile:

For future readers, this is what my configuration.yaml looks like now:

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

sensor:
  - platform: template
    sensors:
      price_level:
        unique_id: electricity_price_level
        value_template: "{{ state_attr('sensor.electricity_price_my_address','price_level') }}"
        
binary_sensor:
  - platform: template
    sensors:
      power_saving:
        unique_id: do_power_saving
        value_template: "{{ state_attr('sensor.electricity_price_my_address','price_level') == 'VERY_EXPENSIVE' }}"
3 Likes

Above seems unfortunately to always return “false” , for some reason ( even if you put “CHEAP” in now ) …
You could just make an binay-sensor with:

value_template: {% if state_attr('sensor.elpris', 'price_level') == 'VERY_EXPENSIVE' %}
  true
{% else %}
  false
{% endif %}

…or skip the (value_template: ) and make an automation

1 Like

Here is the “reasons” for the reported values:

PriceLevel

Price level based on trailing price average (3 days for hourly values and 30 days for daily values)
Value 	Description
NORMAL 	

The price is greater than 90 % and smaller than 115 % compared to average price.
CHEAP 	

The price is greater than 60 % and smaller or equal to 90 % compared to average price.
VERY_CHEAP 	

The price is smaller or equal to 60 % compared to average price.
EXPENSIVE 	

The price is greater or equal to 115 % and smaller than 140 % compared to average price.
VERY_EXPENSIVE 	

The price is greater or equal to 140 % compared to average price.

Thanks! I believe it worked after I reloaded the entities from config, but I can see that the value does not update unless I reload the entities again. It seems to me that binary sensors are not updated along with analog sensor(?). I have no idea, this is all new to me.

I tried the example you posted, but I got an error message saying “missed comma between flow collection entries […]”.

I did this instead:

value_template:  "{{ is_state('sensor.power_price_level', 'VERY_EXPENSIVE') }}"

I have confirmed that the value is updated along with the price.

Thanks again :slight_smile:

For future readers, my configuration.yaml now liks like this:


# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

sensor:
  - platform: template
    sensors:
      power_price_level:
        unique_id: ulf_power_price_level
        value_template: "{{ state_attr('sensor.electricity_price_my_address','price_level') }}"
        
binary_sensor:
  - platform: template
    sensors:
      power_saving:
        unique_id: ulf_power_saving
        value_template:  "{{ is_state('sensor.power_price_level', 'VERY_EXPENSIVE') }}"
1 Like

ok, good to hear, haven’t tried it my self, only in Dev-Tools Templates, and i actually never made a “template binary.sensor” so i don’t know how that works … just playing around, and learning :slight_smile:
PS: about the update/reload, i think i saw something in regards to that somewhere, i’ll let you know if i find it again

1 Like