Configuration.yaml templates,

Hi guys,
Sorry if this has been covered before, but I cant seam to find what Im after,

I have been able to add a custom template into my configuration file, but Im getting a “Duplicate key” error when trying to add another,


template:
  - trigger:
      - platform: time_pattern
        seconds: "/1"
    sensor:
      name: "Dining room show Timer Percentage"
      unique_id: dining_room_show_timer_percentage
      unit_of_measurement: "%"
      state: "{{ (((as_timestamp(state_attr('sensor.dining_room_show_next_timer', 'prior_value')) | float - (as_timestamp(now()) | float) | float) / ((state_attr('sensor.dining_room_show_next_timer', 'sorted_active') | from_json)[0][1].originalDurationInMillis/ 1000)| round(0))*100) | round(0) }}"

template:
  - sensor:
      -name: Home Battery
      state: >
        {% if states("sensor.solarsynk_battery_power")|float >= 0 %}
          Discharge
        {% else %}
          Charge
        {% endif %}

Could some one advise as to what I must do to be able to add more than one custom template sensor?

The time_pattern trigger is working fine, but Im trying to add a new “Home Battery” sensor.

template:
  - trigger:
      - platform: time_pattern
        seconds: "/1"
    sensor:
      name: "Dining room show Timer Percentage"
      unique_id: dining_room_show_timer_percentage
      unit_of_measurement: "%"
      state: "{{ (((as_timestamp(state_attr('sensor.dining_room_show_next_timer', 'prior_value')) | float - (as_timestamp(now()) | float) | float) / ((state_attr('sensor.dining_room_show_next_timer', 'sorted_active') | from_json)[0][1].originalDurationInMillis/ 1000)| round(0))*100) | round(0) }}"

  - sensor:
      -name: Home Battery
      state: >
        {% if states("sensor.solarsynk_battery_power")|float >= 0 %}
          Discharge
        {% else %}
          Charge
        {% endif %}

template: is the integration, it should only appear once in your configuration.yaml file. You can have multiple entities listed (with -) under it.

Thanks, I have removed the second “Template” and running a restart, but Im not seeing it yet as a sensor in states.

Not seeing what?

Got it now,
Was trying to get it up as an entity sensor.

Im trying to run it as a binary sensor (on/off)
So i can set an automaton up with it.

Right now, i can set up a numerical state depending on the battery charge / discharge value, but as im trying to run the automaton to change a wled segment colour, wled keeps crashing.

So im attempting to set this as a simple on/off binary.
Similar to a door sensor that ive already got set up.

Only issue i have now is, i cant run this as a device to set the automaton.

Honest question - is there reason why you’d prefer it written in config.yaml over the UI method of creating template helpers?

You wouldn’t need a restart using this.

Also, you can setup automations based on these template sensors using the entity state rather than a device.

1 Like

To be honest,
As im still extremely new to HA and learning something new every day, i hadnt even through about this.
Ill have a look into it though.

The two templates in my first post are my first real atempts at anything more than the basics.

Then change this:

  - sensor:
      - name: Home Battery
        state: >
          {% if states("sensor.solarsynk_battery_power")|float >= 0 %}
            Discharge
          {% else %}
            Charge
          {% endif %}

To this:

  - binary_sensor:
      - name: Home Battery
        state: >
          {{ states("sensor.solarsynk_battery_power")|float(0) >= 0 }}
1 Like

Ill give this a try after work,
Thank you :+1:

This seams to have worked a treat, Thank you so much for your help.

Just one more question if thats ok?
How would I go about setting a binary sensor up to report HI/MID/LOW depending on a value state? (if its at all possible.)

It is not possible. Binary means “two”. A binary sensor has two states, on or off.

If you want three states use a sensor not a binary sensor. You can still use state triggers and conditions with this.

:man_facepalming: Binary = 1/0…
Ill put the lack of sleep down to that one.

I assume a derivative of my original code would suffice?

@Mutley, if you’re only using it for an automation, you don’t need a specific sensor just to bin the value into HI/MID/LOW.

In my opinion, you could just configure your automation triggers to the exact ranges that you require.

Super Quick Example
alias: Example Automation
mode: single
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.solarsynk_battery_power
    below: 30
    id: LOW
  - platform: numeric_state
    entity_id:
      - sensor.solarsynk_battery_power
    above: 30
    below: 80
    id: MID
  - platform: numeric_state
    entity_id:
      - sensor.solarsynk_battery_power
    above: 80
    id: HI
2 Likes

The only trouble with that is what happens if the value == 30?

It’s not above or below 30.

1 Like

Oh yeah that’s true. Template triggers might be more ergonomic for this case.
*Unless triggers are checked serially.

1 Like
  - sensor:
      -name: Grid Power <500 (GREEN)
      state: >
        {% if states("sensor.solarsynk_grid_power")|float <500 %}
         on
        {% else %}
          off
        {% endif %}


##########################################

        - sensor:
      -name: Grid Power >500 <1000 (AMBER)
      state: >
        {% if states("sensor.solarsynk_grid_power")|float >500 <1000 %}
         on
        {% else %}
          off
        {% endif %}

 #########################################    
         
  - sensor:
      -name: Grid Power >1000 (RED)
      state: >
        {% if states("sensor.solarsynk_grid_power")|float >1000 %}
         on
        {% else %}
          off
        {% endif %}

So this is what Im working on tonight.
Im aiming to work my automations off what sensor returns “ON”.
A Red, Amber or green LED in WLED off an ESP32 to show what grid power usage is occurring at any given time,
(below 500W = Green LED)
between 500 and 1000W returns Amber LED)
Over 1000W to return RED LED)

Right now, the <500 sensor and >1000 Sensor are returning the desired results, but the middle one (>500 <1000) is returning the same end state as the >500 sensor.

What would be your suggestion to return “ON” only on one of the 3 sensors pending on the range value?

Hi,
I did try setting up a numerical state automation to look for a value between Hi and low fixed value, but as im trying to set an LED to return a colour (Green / Amber / Red) between 3 targeted values, the system was getting too confused.
So Im trying to set this sensor up to return ON/OFF instead to run the automation off that.

Well if you’re using ESP Home, you also don’t need to create additional sensors to do this.

I’m not good at ESP Home, but I’m quite sure, if you’re using ESP Home to control your wled on the esp device, you can use lambda to do the exact same thing without having to create any sensors. The esp device is able to subscribe and listen to changes on the states.

I am convinced that trying to parse the value to HI/MID/LO or into on off states is unnecessary.

1 Like

I never thought about ESP Home,
Will have a read up on that, Ive just tried installing the add on, but it’ll not let me.
Ill do some digging too,.