Trigger Template String to float

Hey and good evening guys,

im new to home assistant and trying to get the openhardware sensor working.

The sensor is working but the trigger template doesnt work.

from automation.yaml:

  trigger:
  - platform: template
    value_template: "{{ (states.sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1.state|float) > 37.0 }}"
  condition: []
  action:
  - data:
      entity_id:
      - light.micha_schreibtisch
    service: light.turn_off

I got no errors.
The object exists and get up to date.

But no triggering in the logbook.

Info: In the CPU Temp is above 37, turn of the light.
Manually triggering works und turns off the light.

Would be nice if u have an idea. Tried of combinations of templates.

When posting code, it needs to formatted correctly so others can help you. Look at the blue portion at the top of the page. Paste your code, highlight, then press the button.

The templating tool is a good resource to test whether or not a template provides a good output.

Capture

changed it, now it must be correct formatted

I formatted your automation:

- alias: 'test_automation'
  trigger:
  - platform: template
    value_template: "{{ (states.sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1.state|float) > 37.0 }}"
  action:
  - service: light.turn_off
    entity_id: light.micha_schreibtisc

If it doesn’t work, and you’re sure the sensor is working properly, look at Home Assistant’s States page and confirm the automation is enabled (on).

Here’s another way to define the trigger:

- alias: 'test_automation'
  trigger:
  - platform: numeric_state
    entity_id: sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1
    above: 37
  action:
  - service: light.turn_off
    entity_id: light.micha_schreibtisc

If you paste the following in the template tool, does it give an output?

{{ states.sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1.state | float }}

@123
i copy and paste your code and got the following error in the logs:

Value cannot be processed as a number: <state sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1=43,0; level_0=JVACASPERS, level_1=Intel Core 2 Quad Q9650, level_2=Temperatures, name=CPU Core #1, minimal_value=42,0, maximum_value=54,0, unit_of_measurement=°C, friendly_name=JVACASPERS Intel Core 2 Quad Q9650 Temperatures CPU Core #1 @ 2019-03-31T23:22:52.031088+02:00> (Offending entity: 43,0)

@walrus_parka

i tried to copy and paste your example and the output was : 0.0

the correct value i got from:
{{ states.sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1.state}}

but my example wont work with it

I assume by code you mean the second example using platform: numeric_state? That means the sensor’s value is not understood to be a string and not a numeric value.

Or do you mean the code in the first example using platform: template?

your second example.

Trigger is numeric state.
But it seems, that the output of the
sensor.jvacaspers…core_1
is a string.

so im now trying the following code:

Summary

` trigger:

  • platform: numeric_state
    entity_id: sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1
    value_template: ‘{{ states.sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1.state }}’
    above: 37
    action:
  • service: light.turn_off
    entity_id: light.micha_schreibtisch`

That’s what the filter conversion is for. If you try int instead of float does it change anything?

Please format the code for presentation on this forum.

I suggested you add | float to the end of the value_template's contents:

value_template: "{{ (states.sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1.state|float)}}"

@walrus_parka
Nope. Tried a lot of different combinations.

int,number,float

@123
That was my first try yesterday.

Something is wrong here. If the temperature value is a numeric string, either float or int will convert it to a numeric value. If they are failing to do that then the temperature value is a non-numeric string.

I noticed this: (Offending entity: 43,0)

The comma decimal separator must be due to localization (i.e. that’s the way it’s done in your country). However, is this making Home Assistant treat it as a non-numeric string?!?

@123
i added your " | float" to my value template.

Now Im getting the same error
Value cannot be processed as a number

German here. Decimals are wrote with a commata here.

Is that the problem?

Can you post a screenshot of the entity from the states page?

THANKS A LOT!

The comma as decimal seperator was the problem.

The following template is working:
value_template: "{{ states.sensor.jvacaspers_intel_core_2_quad_q9650_temperatures_cpu_core_1.state |replace(',0', '')| float }}"

The template editor gaves me the correct output as a float:
43.0

and NOT: 43,0

2 Likes

You’re welcome but, to be honest, I don’t understand why Home Assistant fails to understand the localization (i.e. German use of comma as decimal separator).

Nevertheless it is failing to consider localization and is treating 46,0 like a non-numeric string (as non-numeric as 46;0 would be). Strange.

Something needs to be fixed somewhere because I can’t imagine having to use Home Assistant in this manner. For every temperature sensor you’ll need to parse out the comma? That’s ugly! :frowning:

1 Like

Yes but that is okay.

Does ha stops the automation after the first trigger?

Temp goes up -> lamp off
I manually turn on the lamp
…
the lamp stays on. Automation is running

The automation ends after completing the action section.

The interesting thing I discovered, while testing with the Template Editor, is that if I try to convert “46,0” with float it doesn’t fail but simply converts it to zero. My localization uses period as the decimal separator. Try this on your system and see if report zero or produces an error.

Screenshot%20from%202019-03-31%2021-29-54

It does fail because the default of the filter if nothing is set is 0.